

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>开启头部工具栏 - 数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>组件</cite></a>
      <a><cite>数据表格</cite></a>
      <a><cite>开启头部工具栏</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body">
            <div class="layui-tab layui-tab-brief" lay-filter="component-tabs-brief">
              <ul class="layui-tab-title">
                <li class="search_done {if condition='$cid eq false'}layui-this{/if}" cateId="0">全部分类</li>
                {volist name="category" id="v"}                      
                <li  cateId="{$v.id}" class="search_done {if condition='$cid eq $v.id'}layui-this{/if}">{$v.name}</li>
                {/volist} 
                <li><button class="layui-btn layui-btn-sm" id="uploadPic">上传图片</button></li>
              </ul>
              <div class="layui-tab-content">
                <!-- <div class="layui-tab-item layui-show"> -->
                  <!-- <div class="layui-card-header">  </div> -->
                    
                  <table id="list-table" lay-filter="test-table-toolbar">
                  <thead>
                    <tr>
                      <!-- <th lay-data="{type:'checkbox',fixed: 'left', }"></th> -->
                      <th lay-data="{field:'id', width:80, unresize: true, sort: true}">ID</th>
                      <th lay-data="{field:'title'}">附件</th>
                      <th lay-data="{field:'thumb'}">文件名</th>
                      <th lay-data="{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:150}">操作</th>
                    </tr> 
                  </thead>
                  <tbody>
                    {volist name="mess" id="v"}
                    <tr>
                      <!-- <td><input type="checkbox" name=""></td> -->
                      <td>{$v.id}</td>
                      <td><a href="{$v.thumb}" target="_blank" style="color: blue;">{if condition="($v.type eq '.gif') or ($v.type eq '.jpeg') or ($v.type eq '.ico') or ($v.type eq '.png') or ($v.type eq '.jpg')"}<img src="{$v.thumb}">{elseif condition="$v.type eq '.mp4'"/}<video src="{$v.thumb}">您的浏览器不支持 video 标签。</video>{else/}{$v.thumb}{/if}</a></td>     
                      <td>{$v.thumb}</td>      
                      <td>
                    <!--     <a class="layui-btn " lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger " lay-event="del">删除</a> -->
                      </td>
                    </tr>
                    {/volist}                
                  </tbody>
                </table>
                <input type="hidden" id="setVal" value="{$inputId}">
                <!-- </div> -->
          <!--       <div class="layui-tab-item">内容2</div>
                <div class="layui-tab-item">内容3</div>
                <div class="layui-tab-item">内容4</div>
                <div class="layui-tab-item">内容5</div> -->
              </div>
            </div>
            
            <!-- <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar"></table> -->
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
           <!--      <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button> -->
                <button class="layui-btn layui-btn-sm" lay-event="add">上传图片</button>     
 
              </div>
            </script>
             
            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">选择</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="/static/layadmin/layui/layui.js"></script>  
  <script>
  layui.config({
    base: '/static/layadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table','form','laypage'], function(){
    var admin = layui.admin
    ,table = layui.table
    ,laypage  = layui.laypage 
    ,$ = layui.$
    ,form = layui.form;
    
    table.init('test-table-toolbar', {
      // height: 315 //设置高度
      // ,limit: 10 //注意：请务必确保 limit 参数（默认：10）是与你服务端限定的数据条数一致
      //支持所有基础参数
      // toolbar: '#test-table-toolbar-toolbarDemo',
      page: true //开启分页
      // ,limit: 3
    }); 

    
    //头工具栏事件
    table.on('toolbar(test-table-toolbar)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
          var data = checkStatus.data;
          layer.alert(JSON.stringify(data));
        break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
          layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
        case 'add':
        layer.open({
          type: 2, 
          title: '添加', 
          content: '/admin/filex/addArticle', //这里content是一个普通的String
          area: ['100%', '100%']
        });
      };
    });
    
    //监听行工具事件
    table.on('tool(test-table-toolbar)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
          $.get('{:url("admin/filex/index_del")}',{id:data.id},function(ret){
              ret = $.parseJSON(ret);
              if (ret.code == 0) { //添加失败
                  layer.msg(ret.msg, { icon: 5 })
              } else { //添加成功
                  layer.msg(ret.msg, { icon: 6 }, function() {
                      // $(obj).parents("tr").remove();
                      obj.del();
                  })
              }
          });          
          layer.close(index);
        });
      } else if(obj.event === 'edit'){ 
        var inputId=$("#setVal").val(); 
        inputId="#"+inputId;
        $(inputId, window.parent.document).attr("value", data.thumb);//给父页面赋值，很重要
        $(inputId, window.parent.document).attr("subId", data.id);//给父页面赋值，很重要
        // $("#hidThumb", window.parent.document).attr("value", data.id);
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        // layer.alert(JSON.stringify(data.thumb));     
      }
    });
    $('#uploadPic').on('click', function () {
      layer.open({  
          type: 2, 
          title: '添加', 
          content: '/admin/filex/addArticle', //这里content是一个普通的String
          area: ['100%', '100%']
        });
    });
    // 执行搜索，表格重载
    $('.search_done').on('click', function () {
      // 搜索条件
      var cid = $(this).attr("cateId");
      var inputId=$("#setVal").val(); 
      // var status = $('#status').val();
      console.log(cid);
      // console.log(keywords);
      window.location.href="/admin/filex/chose?cid="+cid+"&inputId="+inputId;
    });

  });
  </script>
</body>
</html>